package com.lmc.forum.controller;

import com.lmc.forum.common.AppResult;
import com.lmc.forum.common.ResultCode;
import com.lmc.forum.exception.ApplicationException;
import com.lmc.forum.model.Board;
import com.lmc.forum.services.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
@Tag(name = "版块接口", description = "版块的增删查改")
public class BoardController {

    @Autowired
    private IBoardService boardService;

//    默认为9
    @Value("${forum.index.board-num: 9}")
    private Integer num;

    @GetMapping("/topList")
    @Operation(summary = "获取板块列表")
    public AppResult<List<Board>> topList() {
        log.info("获取板块列表数"+ num);
        List<Board> boards = boardService.selectByNum(num);
        return AppResult.success(boards);
    }

    @GetMapping("/getById")
    @Operation(summary = "获取板块信息")
    public AppResult<Board> getById(
            @Parameter(description = "板块ID", required = true, example = "1")
            @RequestParam(value = "id") @NotNull Long id
    ){
        Board board = boardService.selectById(id);
        if(board == null || board.getDeleteState() == 1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
